"""
simulate_original_pattern.py

Fri Oct 12 14:43:51 CEST 2012

This script simulates the recall of a compelete pattern
with with different seeds at different values of delta_t

To execute type in your terminal
>>> ./simulate_original_pattern 
"""

from network import topology
from firings import Pattern, generate
from firings import get_overlap
from plasticity import clipped_Hebbian
from STDP import delta_t 

import numpy as np

ncells = 3000   # number of cells
c = 0.5         # connection probability
a = 0.1         # activity of a pattern
m = 50          # number of patterns to store
g1 = 0.433      # slope of inhibitory component

def create_synaptic_weights(seed):
    """
    create a matrix of synaptic weigths according to
    the clipped_Hebbian rule describied in Gibson & Robinson 1992

    returns the matrix of connectivity, the original pattern and
    the matrix of synaptic weights
    """

    W = topology(ncells, c, seed)
    Z = Pattern(ncells, a)
    Y = generate(Z, m)

    J = clipped_Hebbian(Y,W)
    return(W, Z, Y, J)

def recall(Z, J):
    """
    simulate 10 progressive recall of the original
    pattern at different dt according to the STDP 
    rule found experimentally
    """
def simulate(seed, dt):
    """
    simulate 10 progressive recall of the original
    pattern at different dt according to the STDP 
    rule found experimentally


    """
    ncells = 3000 # number of cells
    c = 0.5       # connection probability 
    a = 0.1       # activity of a pattern
    m = 50        # number of patterns to store
    g1 = 0.433    # slope of inhibitory component
    
    W = topology(ncells, c, seed)
    Z = Pattern(ncells, a)
    Y = generate(Z, m)

    J = clipped_Hebbian(Y, W)
    J = J*delta_t(dt)


    overlap = np.empty(10)
    X = Y[0] # initial pattern

    for i in range(10):
        h = np.inner(J.T, X)/float(ncells)
        spk_avg = np.mean(X)
        X = ( h < g1*spk_avg ).choose(1,0)
        overlap[i] = get_overlap(Z, X)

    return(overlap)
        

if __name__ == '__main__':
    import sys
    
    mydt = float(sys.argv[1]) # read 1st command argument

    # 50 seeds per simulation
    myseed = np.arange(0, 2500, 50)

    mydata = np.empty( (50,10))
    for i,s in enumerate(myseed):
        mydata[i] = simulate(seed=s, dt = mydt)

    np.savetxt('./data/dt[%s].out'%mydt, mydata, fmt='%f')

    


